package com.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/helloservlet")
public class HellloServlet extends HttpServlet {

    public void HelloServlet(){
        System.out.println("HelloServlet被创建");
    }
    @Override
    public void init() throws ServletException{
        System.out.println("HelloServlet初始化");
    }

    @Override
    protected  void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
            super.service(req,resp);
        System.out.println("servlet被访问");
    }

    @Override
    public void destroy() {
        System.out.println("HelloServlet销毁");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        System.out.println("name="+name);
        String age=req.getParameter("age");
        System.out.println("age="+age);
        //通过相应数据流，向浏览器发送数据
        resp.getWriter().write("你好，我的名字叫"+name+"今年"+age);
//        //实现重定向
//        resp.sendRedirect("https://www.baidu.com?w"+name);
    }

}
